package com.code.juc.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author lyf
 * @date 2021-09-05 下午7:47
 */
@Slf4j
public class MultiThreadTest {

    public static void main(String[] args) {
        // 测试继承Thread
        // testExtendThread();

        // 测试runnable
        testRunnable();

        // 测试callable
        // testCallable();
    }

    private static void testRunnable() {
        MyThreadImplRunnable myThreadImplRunnable = new MyThreadImplRunnable("123");

        Thread thread = new Thread(myThreadImplRunnable);
        thread.start();
    }

    private static void testCallable() {
        MyThreadImplCallable myThreadImplCallable = new MyThreadImplCallable(10);

        ExecutorService executorService= Executors.newFixedThreadPool(1);
        Future<Integer> future = executorService.submit(myThreadImplCallable);
        try {
            log.info("future:{}", future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }

    public static void testExtendThread() {
        // 测试继承Thread
        MyThreadExtendThread myThreadExtendThread1 = new MyThreadExtendThread("test1");
        MyThreadExtendThread myThreadExtendThread2 = new MyThreadExtendThread("test2");

        myThreadExtendThread1.start();
        myThreadExtendThread2.start();
    }

}
